package com.example.administrator.weipiao;

import android.support.v4.app.Fragment;
import android.util.SparseArray;

import fragment.FindFragment;
import fragment.MovieFragment;
import fragment.MyFragment;
import fragment.PerformFragment;

/**
 * Fragment的解析工场，用来滑动的时候选择相对应的页面
 * Created by Administrator on 2016/9/10.
 */
public class FragmentFactory {
    private static final int FRAGMENT_MOVIE = 0;
    private static final int FRAGMENT_PERFORM = 1;
    private static final int FRAGMENT_FIND = 2;
    private static final int FRAGMENT_MY = 3;
    //SparseArray:key是整形的key,根据整形的key来获取一个对象,效率比较高.
    private static SparseArray<Fragment> fragments = new SparseArray<>();
    //创建一个工厂方法，用来创建一个Framgent对象
    public static Fragment createFragment(int index){
        //从集合中获取
        Fragment fragment = fragments.get(index);
        if (fragment==null){
            switch (index){
                case FRAGMENT_MOVIE:
                    fragment = new MovieFragment();
                    break;
                case FRAGMENT_PERFORM:
                    fragment = new PerformFragment();
                    break;
                case FRAGMENT_FIND:
                    fragment = new FindFragment();
                    break;
                case FRAGMENT_MY:
                    fragment = new MyFragment();
                    break;
            }
            fragments.put(index,fragment);

        }
        return fragment;
    }
}
